package com.tbit.uqbike.object.view;

import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.constant.ResponseCode;
import org.springframework.web.servlet.support.RequestContext;

/**
 * @author Kang
 * @description 用户控制层返回json数据的包装类
 */
public class JsonResult {
    private Integer ret;

    private String msgId;
    private String msg;
    private Integer code;
    private Integer recordsTotal;
    private Object data;
    private Object chartData;
    private Integer money;
    private Object totalData;
    private String traceId;


    private Integer num;

    private JsonResult() {

    }


    public JsonResult(Object data, Object chartData) {
        this.data = data;
        this.chartData = chartData;
    }

    public static JsonResult ok() {
        return ok(null, null);
    }

    /**
     * 提供返回数据成功时的方法
     *
     * @param data
     * @return
     */
    public static JsonResult ok(Object data) {
        return ok(data, null);
    }

    public static JsonResult ok(Object data, Integer total) {
        JsonResult rs = new JsonResult();
        rs.ret = ResponseCode.SUCC;
        rs.data = data;
        rs.recordsTotal = total;
        if (rs.getData() == null) {
            rs.setData(new Object());
        }
        return rs;
    }

    /**
     * 提供返回数据出现异常信息的方法
     *
     * @param msgId
     * @return
     */
    public static JsonResult fail(String msgId, Integer code, String msg, String traceId) {
        JsonResult rs = new JsonResult();
        rs.ret = ResponseCode.FAIL;
        rs.msgId = msgId;
        rs.msg = msg;
        rs.code = code;
        rs.traceId = traceId;
        return rs;
    }

    public static JsonResult fail(String msg, String traceId) {
        JsonResult rs = new JsonResult();
        rs.ret = ResponseCode.FAIL;
        rs.msg = msg;
        rs.traceId = traceId;
        return rs;
    }

    public static JsonResult ok(Object data, Integer total, Integer money, Integer num) {
        JsonResult rs = new JsonResult();
        rs.ret = ResponseCode.SUCC;
        rs.data = data;
        rs.recordsTotal = total;
        if (money == null) {
            money = 0;
        }
        rs.money = money;
        rs.num = num;
        if (rs.getData() == null) {
            rs.setData(new Object());
        }

        return rs;
    }

    public static JsonResult ok(Object data, Object totalData) {
        JsonResult rs = new JsonResult();
        rs.ret = ResponseCode.SUCC;
        rs.data = data;
        rs.totalData = totalData;
        if (rs.getData() == null) {
            rs.setData(new Object());
        }
        return rs;
    }

    public static JsonResult ok(Object data, Integer recordsTotal, Object chartData) {
        JsonResult rs = new JsonResult();
        rs.ret = ResponseCode.SUCC;
        rs.data = data;
        rs.recordsTotal = recordsTotal;
        rs.chartData = chartData;
        if (rs.getData() == null) {
            rs.setData(new Object());
        }
        return rs;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    public static JsonResult toAjax(int rows, RequestContext requestContext) {
        return rows > 0 ? JsonResult.ok() : JsonResult.fail(requestContext.getMessage("operation_failed"), TraceUtil.getTraceIdByLocal());
    }

    public Object getChartData() {
        return chartData;
    }

    public void setChartData(Object chartData) {
        this.chartData = chartData;
    }

    public Integer getRet() {
        return ret;
    }

    public void setRet(Integer ret) {
        this.ret = ret;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Object getTotalData() {
        return totalData;
    }

    public void setTotalData(Object totalData) {
        this.totalData = totalData;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public Integer getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(Integer recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
